package com.esm.view;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.esm.app.MyApplication;
import com.esm.netity.BadlyTongji;
import com.esm.tool.BaseActivity;
import com.esm.tool.MylisAdapter;
import com.esm.tool.MyspinAdapter;
import com.esm.tool.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class SecondActivity extends BaseActivity implements OnClickListener {
    private final String url = MyApplication.SERVERPATH + "/Y502/GetBadlyinfowz";
    private final String urll = MyApplication.SERVERPATH + "/Y502/GetindexBadwz";
    private final String wurl = MyApplication.SERVERPATH + "/Y502/Getbadlyinfobyweizhi";
    private final String wurll = MyApplication.SERVERPATH + "/Y502/Getbadlyinfobyweizhi2";
    private final String burl = MyApplication.SERVERPATH + "/Y502/Getbadlyinfobybl";
    private final String burll = MyApplication.SERVERPATH + "/Y502/Getbadlyinfobybl2";
    private final String lurl = MyApplication.SERVERPATH + "/Y502/Getbadlyinfobyleixing";
    private final String lurll = MyApplication.SERVERPATH + "/Y502/Getbadlyinfobyleixing2";
    RequestQueue mQueue = null;
    JsonObjectRequest mJsonObjectRequest = null;
    ArrayList<BadlyTongji> list1 = new ArrayList<BadlyTongji>();
    ArrayList<BadlyTongji> list2 = new ArrayList<BadlyTongji>();
    String riqi = "";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    ArrayList<String> m = null;
    private ListView lv1;
    private ListView lv2;
    private MylisAdapter adapter = null;
    private MylisAdapter adapter1 = null;
    private String uu = "", uu1 = "", uu2;
    private String po = "";
    private int xy = 0;//
    private ImageView image;
    private String sn;
    private Spinner spinner;
    private TextView id;
    private TextView id2;
    private TextView code;
    private TextView num;
    private TextView name;
    private TextView weizhi;
    private int spnum = 0;
    private MyspinAdapter adapter4, adapter5;
    private Spinner spinner0;

    private String sid, slx, smc, swz, sdm, ssl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_second2);

        String sta = MyApplication.yy;
        updateActivity(sta, SecondActivity.class);

        m = new ArrayList<>();
        sid = this.getString(R.string.id);
        slx = this.getString(R.string.yclx);
        smc = this.getString(R.string.ycmc);
        swz = this.getString(R.string.weizhi);
        sdm = this.getString(R.string.jcdm);
        ssl = this.getString(R.string.ycsl);
        m.add(sid);
        m.add(slx);
        m.add(smc);
        m.add(swz);
        long thistime = MyApplication.xtime * 1000 + System.currentTimeMillis();
        riqi = df.format(new Date(thistime));
        mQueue = MyApplication.getRequestQueue();
        sn = getIntent().getStringExtra("sn");
        if (MyApplication.slist.size() > 0)
            po = MyApplication.slist.get(0);
        uu1 = url + "?sn=" + sn + "&&riqi=" + riqi + "&&model=badxiehao";
        setView();

    }

    private void setView() {
        lv1 = (ListView) findViewById(R.id.second_lv1);
        adapter = new MylisAdapter(this, list1);
        lv1.setAdapter(adapter);
        lv2 = (ListView) findViewById(R.id.second_lv2);
        adapter1 = new MylisAdapter(this, list2);
        lv2.setAdapter(adapter1);
        image = (ImageView) findViewById(R.id.second_return);
        image.setOnClickListener(this);
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner0 = (Spinner) findViewById(R.id.spinner0);
        adapter5 = new MyspinAdapter(this, MyApplication.slist);
        spinner0.setAdapter(adapter5);
        adapter4 = new MyspinAdapter(this, m);
        spinner.setAdapter(adapter4);
        spinner.setSelection(0);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                spnum = position;
                MyApplication.index = -1;
                String s1 = m.get(position);
                if (s1.equals(sid)) {

                    code.setText(slx);
                    name.setText(smc);
                    weizhi.setText(swz);
                    uu1 = url + "?sn=" + sn + "&&riqi=" + riqi + "&&model=badxiehao";
                    uu = uu1 + "&&po=" + po;//uu=url+"?sn="+sn+"&&po="+po+"&&riqi="+riqi+"&&model=badxiehao";
                    getData(uu, "xiehao");
                } else if (s1.equals(slx)) {
                    code.setText(slx);
                    name.setText(sdm);
                    weizhi.setText(ssl);
                    uu1 = lurl + "?sn=" + sn + "&&riqi=" + riqi;
                    uu = uu1 + "&&po=" + po;
                    getData(uu, "xiehao");
                } else if (s1.equals(smc)) {
                    code.setText(smc);
                    name.setText(sdm);
                    weizhi.setText(swz);
                    uu1 = burl + "?sn=" + sn + "&&riqi=" + riqi;
                    uu = uu1 + "&&po=" + po;
                    getData(uu, "xiehao");
                } else if (s1.equals(swz)) {
                    code.setText(swz);
                    name.setText(smc);
                    weizhi.setText(ssl);
                    uu1 = wurl + "?sn=" + sn + "&&riqi=" + riqi;
                    uu = uu1 + "&&po=" + po;
                    getData(uu, "xiehao");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner0.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                po = MyApplication.slist.get(position);
                uu = uu1 + "&&po=" + po;
                getData(uu, "xiehao");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        id = (TextView) findViewById(R.id.second_id);
        id2 = (TextView) findViewById(R.id.second_id2);
        code = (TextView) findViewById(R.id.second_code);
        num = (TextView) findViewById(R.id.second_num);
        name = (TextView) findViewById(R.id.second_name);
        weizhi = (TextView) findViewById(R.id.second_weizhi);
        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!list1.get(position).getZt().equals("1")) {
                    try {//MyApplication.index2=MyApplication.index;
                        list1.get(position).setZt("1");
                        if (MyApplication.index >= 0)
                            list1.get(MyApplication.index).setZt("0");
                        MyApplication.index = position;
                        adapter.notifyDataSetChanged();
                        String n = "";
                        n = list1.get(position).getCode();
                        if (spnum == 2)
                            n = list1.get(position).getCode1();
                        uu2 = urll + "?badname=" + n;
                        xy = 0;
                        if (spnum == 0) {
                            xy = 1;
                        } else if (spnum == 1) {
                            n = URLEncoder.encode(n, "UTF-8");
                            uu2 = lurll + "?leixing=" + n;
                            xy = 1;
                        } else if (spnum == 2) {
                            n = URLEncoder.encode(n, "UTF-8");
                            uu2 = burll + "?code=" + n;
                        } else if (spnum == 3) {
                            n = URLEncoder.encode(n, "UTF-8");
                            uu2 = wurll + "?weizhi=" + n;
                            xy = 1;
                        }
                        uu = uu2 + "&&po=" + po;
                        String s9 = uu;
                        getData(uu, "dg");
                    } catch (UnsupportedEncodingException e) {

                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void getData(String uu, final String model) {

        boolean isConnected = Utils.toastIsNetworkConnected(context);
        if (isConnected) {
            showProgressBar();
            mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uu,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    hideProgressBar();
                    parseData(response, model);
                }


            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    hideProgressBar();
                    showToast(getString(R.string.network_error_try_again));
                }
            }
            );
            mQueue.add(mJsonObjectRequest);
        } else {
            showToast(getString(R.string.network_error_try_again));
            return;
        }
    }


    public void parseData(final JSONObject response, final String model) {

        runOnUiThread(new Runnable() {
            public void run() {


                String re = null;
                try {
                    re = response.getString("result");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (re.equals("yes")) {
                    JSONArray array = response.optJSONArray("data");
                    if (array != null && model.equals("xiehao")) {
                        list1.clear();
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject obj = array.optJSONObject(i);
                            BadlyTongji b = new BadlyTongji();
                            if (spnum == 2) {
                                if (MyApplication.yy.equals("zh"))
                                    b.setCode(obj.optString("name"));
                                else if (MyApplication.yy.equals("en"))
                                    b.setCode(obj.optString("nameen"));
                                else
                                    b.setCode(obj.optString("namevi"));
                                b.setCode1(obj.optString("name"));
                            } else
                                b.setCode(obj.optString("name"));
                            b.setNum(obj.optString("num"));
                            b.setZt("0");
                            list1.add(b);
                        }
                        adapter.notifyDataSetChanged();
                    } else if (array != null && model.equals("dg")) {
                        list2.clear();
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject obj = array.optJSONObject(i);
                            BadlyTongji b = new BadlyTongji();
                            if (xy == 1) {
                                if (MyApplication.yy.equals("zh"))
                                    b.setCode(obj.optString("name"));
                                else if (MyApplication.yy.equals("en"))
                                    b.setCode(obj.optString("nameen"));
                                else
                                    b.setCode(obj.optString("namevi"));
                            } else
                                b.setCode(obj.optString("name"));
                            b.setNum(obj.optString("num"));
                            b.setZt("0");
                            list2.add(b);
                        }
                        adapter1.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.second_return:
                if (MyApplication.index >= 0)
                    list1.get(MyApplication.index).setZt("0");
                MyApplication.index = -2;
                this.finish();
        }

    }
}
